package com.huatai.datacenter.config;

import com.huatai.common.util.CollectionUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @program: ht-system
 * @description: opentsdb
 * @author: songxiude
 * @create: 2023-04-25 15:00
 **/
@Configuration
@ConfigurationProperties(prefix = "opentsdb")
@Data
public class ServerInfo {

	/**
	 * ip地址
	 */
	private String ip;
	/**
	 * 端口
	 */
	private String port;
	/**
	 * 基础查询
	 */
	private String uriQuery = "/api/query";
	/**
	 * 查询最新一条记录
	 */
	private String uriQueryLast = "/api/query/last";
	/**
	 * 写入
	 * details 返回详细信息
	 */
	private String uriPut = "/api/put?details";

	/**
	 * 汇总数据写入
	 */
	private String uriRollupPut = "/api/rollup?details";

	/**
	 * 查询时出现的错误
	 */
	private String[] errorMessage = {"{\"error\"", "null", "<!D"};

	public String getQueryUrl() {
		return ip + ":" + port + uriQuery;
	}

	public String getQueryLastUrl() {
		return ip + ":" + port + uriQueryLast;
	}

	public String getPutUrl() {
		return ip + ":" + port + uriPut;
	}

	public String getRollupPutUrl() {
		return ip + ":" + port + uriRollupPut;
	}

	public String[] getErrorMessage() {
		Set<String> result = new HashSet<>();
		result.add("{\"error\"");
		result.add("null");
		result.add("<!D");
		if (errorMessage == null || errorMessage.length == 0) {
			List<String> list = CollectionUtil.arrayToList(errorMessage);
			result.addAll(list);
		} else if (errorMessage[0].startsWith("$")) {
			return result.toArray(new String[3]);
		}
		return result.toArray(new String[3]);
	}
}
